#include "stdio.h"
#include "printf_test.h"
#include "types.h"

// 定义一些边界值
#define INT_MAX 2147483647
#define INT_MIN (-2147483648)

/**
 * @brief 1. 测试基本的格式化功能
 * - %d: 正数、负数、零
 * - %x: 十六进制
 * - %s: 普通字符串
 * - %c: 单个字符
 * - %%: 百分号转义
 */
void test_printf_basic(void) {
    printf("--- Test Case 1: Basic Formatting ---\n");
    printf("Integer: %d\n", 42);
    printf("Negative Integer: %d\n", -123);
    printf("Zero: %d\n", 0);
    printf("Hexadecimal: 0x%x\n", 0xDEADBEEF);
    printf("String: %s\n", "Hello, World!");
    printf("Character: %c\n", 'A');
    printf("Percent Sign: %%\n");
    printf("\n");
}

/**
 * @brief 2. 测试边界条件处理
 * - 整数最大/最小值
 * - NULL 字符串指针
 * - 空字符串
 */
void test_printf_boundary(void) {
    printf("--- Test Case 2: Boundary Conditions ---\n");
    printf("Integer MAX: %d\n", INT_MAX);
    printf("Integer MIN: %d\n", INT_MIN);
    printf("String (NULL): %s\n", (char*)0);
    printf("String (Empty): %s\n", "");
    printf("\n");
}

/**
 * @brief 3. 性能测试（大量输出）
 * - 连续输出多行，测试缓冲区和 UART 的处理能力
 */
void test_printf_performance(void) {
    printf("--- Test Case 3: Performance (Bulk Output) ---\n");
    for (int i = 0; i < 10; i++) {
        printf("Line %d: This is a test line for performance evaluation. The quick brown fox jumps over the lazy dog.\n", i);
    }
    printf("\n");
}

/**
 * @brief 4. 错误恢复/异常情况测试
 * - 无效的格式化符号（行为未定义，但可观察其健壮性）
 * - 混合有效和无效的符号
 */
void test_printf_error(void) {
    printf("--- Test Case 4: Error/Recovery ---\n");
    printf("Invalid specifier: %y\n", 123); // %y 是无效的
    printf("Mixed: %d, %z, %s\n", 42, "this should not print", "and this should");
    printf("\n");
}

/**
 * @brief 运行所有 printf 测试套件
 */
void run_all_printf_tests(void) {
    printf("========================================\n");
    printf("      Starting printf Test Suite        \n");
    printf("========================================\n\n");

    test_printf_basic();
    test_printf_boundary();
    test_printf_performance();
    test_printf_error();

    printf("========================================\n");
    printf("       printf Test Suite Finished       \n");
    printf("========================================\n\n");
}
